package com.wsc.wsckeeper.activities;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.base.BaseActivity;

public class ChooseFileActivity extends BaseActivity {
	public static String CURRECT_FILE;
	private ListView list;
	private List<String> item;

	private static final String ITEM_KEY = "key";
	private static final String ITEM_IMAGE = "image";
	private static final String ROOT = "/";
	private static final String UP = "../";

	private List<String> path = null;
	private ArrayList<HashMap<String, Object>> mList;

	private String currentPath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_choose_file);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initComponents() {
		list = (ListView) findViewById(R.id.list);
	}

	@Override
	public void initValues() {
		getDirImpl(getIntent().getExtras().getString(CURRECT_FILE, ROOT));
	}

	@Override
	public void initListeners() {
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				File file = new File(path.get(position));
				if (file.isDirectory() && file.canRead()) {
					getDirImpl(path.get(position));
				}
			}
		});

		list.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				String current = item.get(position);
				if (!current.equalsIgnoreCase(ROOT) && !current.equalsIgnoreCase(UP)) {
					current = currentPath + "/" + current;
					Intent intent = new Intent();
					intent.putExtra(ChooseFileActivity.CURRECT_FILE, current);
					getActionBar().setSubtitle(current);
					setResult(RESULT_OK, intent);
					finish();
				}
				return false;
			}
		});
	}

	private void getDirImpl(final String dirPath) {
		getActionBar().setSubtitle(dirPath);
		currentPath = dirPath;

		item = new ArrayList<String>();
		path = new ArrayList<String>();
		mList = new ArrayList<HashMap<String, Object>>();

		File f = new File(currentPath);
		File[] files = f.listFiles();
		if (files == null) {
			currentPath = ROOT;
			f = new File(currentPath);
			files = f.listFiles();
		}

		if (!currentPath.equals(ROOT)) {
			item.add(ROOT);
			addItem(ROOT, R.drawable.folder);
			path.add(ROOT);

			item.add(UP);
			addItem(UP, R.drawable.folder);
			path.add(f.getParent());
		}

		TreeMap<String, String> dirsMap = new TreeMap<String, String>();
		TreeMap<String, String> dirsPathMap = new TreeMap<String, String>();
		TreeMap<String, String> filesMap = new TreeMap<String, String>();
		TreeMap<String, String> filesPathMap = new TreeMap<String, String>();
		for (File file : files) {
			if (file.isDirectory() && file.canRead()) {
				String dirName = file.getName();
				dirsMap.put(dirName, dirName);
				dirsPathMap.put(dirName, file.getPath());
			}
		}
		item.addAll(dirsMap.tailMap("").values());
		item.addAll(filesMap.tailMap("").values());
		path.addAll(dirsPathMap.tailMap("").values());
		path.addAll(filesPathMap.tailMap("").values());

		SimpleAdapter adapter = new SimpleAdapter(this, mList, R.layout.adapter_item_choose_file, new String[] { ITEM_KEY, ITEM_IMAGE }, new int[] { R.id.fdrowtext, R.id.fdrowimage });
		for (String dir : dirsMap.tailMap("").values()) {
			addItem(dir, R.drawable.folder);
		}
		for (String file : filesMap.tailMap("").values()) {
			addItem(file, R.drawable.folder);
		}
		adapter.notifyDataSetChanged();
		list.setAdapter(adapter);
	}

	private void addItem(String fileName, int imageId) {
		HashMap<String, Object> item = new HashMap<String, Object>();
		item.put(ITEM_KEY, fileName);
		item.put(ITEM_IMAGE, imageId);
		mList.add(item);
	}
}
